package cn.ffcs.uec.staff.component;

import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.Div;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.Window;

import cn.ffcs.uec.staff.component.bean.UecStaffInfoListboxCompBean;
import cn.ffcs.uec.staff.manager.UecStaffInfoManager;
import cn.ffcs.uec.staff.model.UecStaffInfo;
import cn.ffcs.uec.staff.vo.UecStaffInfoVo;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;
import lombok.Getter;
import lombok.Setter;

/**
 * 组织树下员工查询.
 * 
 * @version Revision 1.0.0
 */
@Controller
@Scope("prototype")
public class UecStaffInfoListboxComp extends Div implements IdSpace {
	private static final long serialVersionUID = -692508647557168333L;

	/**
	 * bean.
	 */
	private UecStaffInfoListboxCompBean bean = new UecStaffInfoListboxCompBean();

	/**
	 * zul.
	 */
	private final String zul = "/pages_nmg/uec_staff/comp/uec_staff_info_listbox.zul";

	private UecStaffInfoManager uecStaffInfoManager = (UecStaffInfoManager) ApplicationContextUtil
			.getBean("uecStaffInfoManager");

	/**
	 * UecStaffInfo
	 */
	private UecStaffInfo uecStaffInfo;
	/**
	 * 合同制、派遣制人员虚拟对象
	 */
	private UecStaffInfoVo uecStaffInfoVo;

	/**
	 * 操作类型
	 */
	private String opType;
	/**
	 * 
	 */
	private boolean salaryChangesFlag = false;
	/**
	 * 
	 */
	private boolean salaryChangesCleanFlag = true;

	@Getter
	@Setter
	private IPortletInfoProvider portletInfoProvider;

	public UecStaffInfoListboxComp() {
		Executions.createComponents(this.zul, this, null);
		Components.wireVariables(this, bean);
		Components.addForwards(this, this, '$');
		// 转发
		this.addForward("onOrgTreeStaffQuery", this, "onOrgTreeStaffQueryResponse");
		this.addForward("onSalaryChangesFlag", this, "onSalaryChangesFlagResponse");
	}

	/**
	 * 初始化
	 * 
	 * @throws ParseException
	 */
	public void onCreate() throws ParseException {
		// 防止直接查询报错
		uecStaffInfoVo = new UecStaffInfoVo();
		
		onQueryStaff();
	}

	/**
	 * 接收抛出的查询事件
	 * 
	 * @param event
	 */
	public void onOrgTreeStaffQueryResponse(final ForwardEvent event) {
		uecStaffInfoVo = (UecStaffInfoVo) event.getOrigin().getData();
		if (null != uecStaffInfoVo) {
			this.bean.getName().setValue(null);
			this.bean.getCode().setValue(null);
			
			uecStaffInfoVo.setStaffName(null);
			uecStaffInfoVo.setStaffCode(null);
			
			// 执行查询
			onQueryStaff();
		}
	}
	
	public void onSalaryChangesFlagResponse(final ForwardEvent event) {
		salaryChangesFlag = (Boolean) event.getOrigin().getData();
	}
	
	

	/**
	 * onDoubleClick=onUecStaffInfoSelectRequest 双击选择员工.
	 * 
	 * @throws Exception
	 *             异常
	 */
	public void onUecStaffInfoSelectRequest() throws Exception {
		if (this.bean.getUecStaffInfoListbox().getSelectedCount() > 0) {
			uecStaffInfoVo = (UecStaffInfoVo) this.bean.getUecStaffInfoListbox().getSelectedItem().getValue();
			if (uecStaffInfoVo != null) {
				/**
				 * 将获取到的虚拟对象转化为UecOutStaffInfo
				 */
				uecStaffInfo = UecStaffInfo.newInstance();
				uecStaffInfo.setPkPsndoc(uecStaffInfoVo.getStaffId());
				uecStaffInfo.setCode(uecStaffInfoVo.getStaffCode());
				uecStaffInfo = uecStaffInfoManager.findUecStaffInfo(uecStaffInfo);
				if (uecStaffInfo != null) {
					if(!salaryChangesFlag) {
						onUecStaffInfoView();
					}else {
						//将事件抛给父类--UecStaffSalaryChangesMainComposer
						Events.postEvent("onSalaryChangeUecStaffInfoQuery", this, uecStaffInfo);
					}
				}
			}
		}
	}

	/**
	 * 每页显示
	 * 
	 * @throws Exception
	 */
	public void onPageSizeSelect() throws Exception {
		if (this.bean.getPageListbox() != null && this.bean.getPageListbox().getSelectedItem().getValue() != null) {
			this.bean.getUecStaffInfoListPaging().setActivePage(0);
			this.bean.getUecStaffInfoListPaging()
					.setPageSize(Integer.parseInt(this.bean.getPageListbox().getSelectedItem().getValue().toString()));
		}
		this.onQueryUecStaffInfo();
	}

	/**
	 * 重置按钮
	 */
	public void onResetStaff() {
		clearVariable();
		this.bean.getTotalSize().setValue(null);
		this.bean.getName().setValue(null);
		this.bean.getCode().setValue(null);
		ListboxUtils.clearListbox(this.bean.getUecStaffInfoListbox());
		this.bean.getUecStaffInfoListPaging().setTotalSize(1);
		// 防止重置后直接查询报错
		uecStaffInfoVo = new UecStaffInfoVo();
		
		Events.postEvent("onCleanSalaryChangeUecStaffInfoQuery", this, salaryChangesCleanFlag);
	}

	/**
	 * 清除变量
	 */
	public void clearVariable() {
		uecStaffInfoVo = null;
		uecStaffInfo = null;
	}

	/**
	 * 查询按钮
	 */
	public void onQueryStaff() {
		if (!StrUtil.isEmpty(this.bean.getCode().getValue())) {
			uecStaffInfoVo.setStaffCode(this.bean.getCode().getValue());
		}
		if (!StrUtil.isEmpty(this.bean.getName().getValue())) {
			uecStaffInfoVo.setStaffName(this.bean.getName().getValue());
		}
		onQueryUecStaffInfo();
	}

	/**
	 * 查询员工信息
	 * 
	 * @throws Exception
	 *             异常
	 */
	public void onQueryUecStaffInfo() {
		if (this.uecStaffInfoVo != null) {
			int activePage = bean.getUecStaffInfoListPaging().getActivePage();
			int pageSize = bean.getUecStaffInfoListPaging().getPageSize();
			PageInfo pageInfo = uecStaffInfoManager.queryPageInfoByUecStaffInfoVo(uecStaffInfoVo, activePage + 1,
					pageSize);
			ListModel list = new BindingListModelList(pageInfo.getDataList(), true);
			this.bean.getUecStaffInfoListbox().setModel(list);
			//总条数
			bean.getTotalSize().setValue(String.valueOf(pageInfo.getTotalCount()));
			this.bean.getUecStaffInfoListPaging().setTotalSize(pageInfo.getTotalCount());
		}
	}

	/**
	 * 查看员工信息
	 * 
	 * @throws Exception
	 */
	public void onUecStaffInfoView() throws Exception {
		opType = "view";
		this.openUecStaffInfoViewWin();
	}

	/**
	 * 打开员工信息查看窗口.
	 * 
	 * @param opType
	 *            操作类型
	 * @throws Exception
	 *             异常
	 */
	private void openUecStaffInfoViewWin() throws Exception {
		Map<String, Object> arg = new HashMap<String, Object>();
		arg.put("opType", opType);
		arg.put("uecStaffInfo", uecStaffInfo);
		Window win = (Window) Executions.createComponents("/pages_nmg/uec_staff/uec_staff_main.zul", this, arg);
		win.doModal();
	}
}
